package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.zhgd.util.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.domain.NSysStation;
import com.ruoyi.zhgd.domain.ZjHntScaleRef;
import com.ruoyi.zhgd.dto.ZjHntMaterialStoreDto;
import com.ruoyi.zhgd.mapper.NSysStationMapper;
import com.ruoyi.zhgd.mapper.ZjHntScaleRefMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.ZjHntMaterialStoreBo;
import com.ruoyi.zhgd.domain.vo.ZjHntMaterialStoreVo;
import com.ruoyi.zhgd.domain.ZjHntMaterialStore;
import com.ruoyi.zhgd.mapper.ZjHntMaterialStoreMapper;
import com.ruoyi.zhgd.service.IZjHntMaterialStoreService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 料仓  自建-混凝土-料仓Service业务层处理
 *
 * @author yhy
 * @date 2023-09-22
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class ZjHntMaterialStoreServiceImpl implements IZjHntMaterialStoreService {

    private final ZjHntMaterialStoreMapper baseMapper;

    @Autowired
    private NSysStationMapper stationMapper;

    @Autowired
    private ZjHntScaleRefMapper zjHntScaleRefMapper;

    /**
     * 查询料仓  自建-混凝土-料仓
     */
    @Override
    public ZjHntMaterialStoreVo queryById(Long id){
//        baseMapper.selectVoById(id);
        return baseMapper.queryById(id);
    }

    /**
     * 查询料仓  自建-混凝土-料仓列表
     */
    @Override
    public TableDataInfo<ZjHntMaterialStoreVo> queryPageList(ZjHntMaterialStoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZjHntMaterialStore> lqw = buildQueryWrapper(bo);
        Page<ZjHntMaterialStoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询料仓  自建-混凝土-料仓列表
     */
    @Override
    public List<ZjHntMaterialStoreVo> queryList(ZjHntMaterialStoreBo bo) {
        LambdaQueryWrapper<ZjHntMaterialStore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZjHntMaterialStore> buildQueryWrapper(ZjHntMaterialStoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZjHntMaterialStore> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMaterialStoreNumber() != null, ZjHntMaterialStore::getMaterialStoreNumber, bo.getMaterialStoreNumber());
        lqw.like(StringUtils.isNotBlank(bo.getStationName()), ZjHntMaterialStore::getStationName, bo.getStationName());
        lqw.like(StringUtils.isNotBlank(bo.getMatchingName()), ZjHntMaterialStore::getMatchingName, bo.getMatchingName());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialStoreName()), ZjHntMaterialStore::getMaterialStoreName, bo.getMaterialStoreName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialStoreType()), ZjHntMaterialStore::getMaterialStoreType, bo.getMaterialStoreType());
        return lqw;
    }

    /**
     * 新增料仓  自建-混凝土-料仓
     */
    @Override
    public Boolean insertByBo(ZjHntMaterialStore bo) {
        ZjHntMaterialStore add = BeanUtil.toBean(bo, ZjHntMaterialStore.class);
        NSysStation nSysStation = stationMapper.selectByIdentifier(bo.getIdentifier());
        bo.setStationName(nSysStation.getName());
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改料仓  自建-混凝土-料仓
     */
    @Override
    public Boolean updateByBo(ZjHntMaterialStore bo) {
        ZjHntMaterialStore update = BeanUtil.toBean(bo, ZjHntMaterialStore.class);
        NSysStation nSysStation = stationMapper.selectByIdentifier(bo.getIdentifier());
        ZjHntScaleRef ref = new ZjHntScaleRef();
        ref.setMaterialStoreType(bo.getMaterialStoreType());
        ref.setMaterialStoreName(bo.getMaterialStoreName());
        QueryWrapper<ZjHntScaleRef> wrapper = new QueryWrapper<>();
        wrapper.eq("station_id",bo.getId());
        zjHntScaleRefMapper.update(ref,wrapper);
        bo.setStationName(nSysStation.getName());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZjHntMaterialStore entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除料仓  自建-混凝土-料仓
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public PageUtils hntMaterialStoreList(Page page, Map<String, Object> params) {
        List<Map> materialStoreList = baseMapper.hntMaterialStoreList(page, params);
        return new PageUtils(page.setRecords(materialStoreList));
    }

    @Override
    public List<ZjHntMaterialStoreDto> selectMaterialStoreType(Map<String, Object> params) {
        List<ZjHntMaterialStoreDto> zjHntMaterialStoreDtos = baseMapper.selectMaterialStoreType(params);
        return zjHntMaterialStoreDtos;
    }
}
